package cn.edu.wynu.annotation;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

// @Component作用相当于配置文件里面的 <bean>
@Component
public class DogAnnotation {
    // @Value 相当于之前的<property name="name" value="小张"></property>
    @Value("小张")
    String name;

    @Value("18")
    Integer age;

    public DogAnnotation() {
        System.out.println("构造器调用");
    }

    public DogAnnotation(String name, Integer age) {
        this.name = name;
        this.age = age;
        System.out.println("全参构造器调用!");
    }

    @PostConstruct
    public void myInitFunc() {
        System.out.println("初始化方法调用!");
    }

    @Override
    public String toString() {
        return "DogAnnotation{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void setName(String name) {
        System.out.println("set方法调用");
        this.name = name;
    }

    public void setAge(Integer age) {
        System.out.println("set方法调用");
        this.age = age;
    }

    @PreDestroy
    public void myDestroy() {
        System.out.println("销毁方法调用");
    }

    public static void main(String[] args) {
        // 相当于 ClassPathXmlApplicationContext
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(MyConfig.class);

        DogAnnotation bean = context.getBean(DogAnnotation.class);

        // 默认的名字是类名的首字母小写
        Object bean1 = context.getBean("dogAnnotation");
        context.close();
        System.out.println(bean);
        System.out.println(bean1);
    }
}
